﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class admin_module_Comment_manager : System.Web.UI.UserControl
{
    CommentBus cmtBus;
    Article_BU articleBus;
    int n;
    protected void Page_Load(object sender, EventArgs e)
    {
        cmtBus = new CommentBus();
        articleBus = new Article_BU();
        if (!IsPostBack)
        {
            getComment();
        }

    }

    public void getComment()
    {
        DataTable dt = cmtBus.getAllComment();

        DataTable dtCurrent = new DataTable();
        dtCurrent.Columns.Add("comment_id");
        dtCurrent.Columns.Add("email");
        dtCurrent.Columns.Add("article_name");
        dtCurrent.Columns.Add("comment_name");
        dtCurrent.Columns.Add("comment_des");
        dtCurrent.Columns.Add("status");
        dtCurrent.Columns.Add("date_added");
        DataRow row;
        foreach (DataRow r in dt.Rows)
        {
            
                int article_id = Convert.ToInt32(r["article_id"].ToString());
                DataTable dtArticle = articleBus.Get_Article_By_Id_BU(article_id);
                row = dtCurrent.NewRow();
                row["comment_id"] = r["comment_id"];
                row["email"] = r["email"];
                row["article_name"] = dtArticle.Rows[0]["article_name"].ToString();
                row["comment_name"] = r["comment_name"];
                row["comment_des"] = r["comment_des"];
                bool status = Convert.ToBoolean(r["status"].ToString());
                if (status)
                {
                    row["status"] = "Bật";
                }
                else
                {
                    row["status"] = "Tắt";
                }
                row["date_added"] = r["date_added"];
                dtCurrent.Rows.Add(row);
        }
        DataList1.DataSource = dtCurrent;
        DataList1.DataBind();

    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = e.Item.ItemIndex;
        this.DataList1.DataBind();
        DataTable dt = cmtBus.getAllComment();
        DataTable dtCurrent = new DataTable();
        dtCurrent.Columns.Add("comment_id");
        dtCurrent.Columns.Add("email");
        dtCurrent.Columns.Add("article_id");
        dtCurrent.Columns.Add("article_name");
        dtCurrent.Columns.Add("comment_name");
        dtCurrent.Columns.Add("comment_des");
        dtCurrent.Columns.Add("status");
        dtCurrent.Columns.Add("date_added");
        DataRow row;
       
        foreach (DataRow r in dt.Rows)
        {
            int article_id = Convert.ToInt32(r["article_id"].ToString());
            DataTable dtArticle = articleBus.Get_Article_By_Id_BU(article_id);
            row = dtCurrent.NewRow();
            row["comment_id"] = r["comment_id"];
            row["email"] = r["email"];
            row["article_id"] = r["article_id"];
            row["article_name"] = dtArticle.Rows[0]["article_name"].ToString();
            row["comment_name"] = r["comment_name"];
            row["comment_des"] = r["comment_des"];
            bool status = Convert.ToBoolean(r["status"].ToString());
            if (status)
            {
                row["status"] = "Bật";
            }
            else 
            {
                row["status"] = "Tắt";
            }

            row["date_added"] = r["date_added"];
            dtCurrent.Rows.Add(row);
        }
        DataList1.DataSource = dtCurrent;
        DataList1.DataBind();

    }

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    }
    protected void imgSave_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void imgCancel_Click(object sender, ImageClickEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        getComment();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        Comment cmt = new Comment();
        cmt.Comment_id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
        cmt.Article_id = int.Parse(((Label)e.Item.FindControl("lblArticle_id")).Text.ToString());
        cmt.Comment_name = ((TextBox)e.Item.FindControl("txtComment_name")).Text.ToString();
        cmt.Comment_des = ((TextBox)e.Item.FindControl("txtComment_des")).Text.ToString();
        cmt.Email = ((Label)e.Item.FindControl("lblEmail")).Text.ToString();
        string status = ((DropDownList)e.Item.FindControl("ddlStatus")).SelectedValue;
        if (status == "") 
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Tin Tuc Online", "<script>alert('Bạn chưa chọn trạng thái của comment này')</script>", false);
            return;
        }
        cmt.Status = Convert.ToBoolean(status);
        if (cmtBus.Insert_UpdateComment(cmt))
        {
            DataList1.EditItemIndex = -1;
            getComment();
        }
        else 
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Tin Tuc Online", "<script>alert('Sửa lỗi')</script>", false);
        }


    }
}